-- keys: *train_Id(redis-key)
-- args seatTypeCountArrayJSONStr, takeoutRouteDTOListStr

local remainTicketPrefix = KEYS[2]

local seatTypeCountArrayJSONStr = ARGV[1]
local seatTypeCountArray = cjson.decode(seatTypeCountArrayJSONStr)

local takeoutRouteDTOListStr = ARGV[2]
local takeoutRouteDTOList = cjson.decode(takeoutRouteDTOListStr)

for index, seatTypeCount in ipairs(seatTypeCountArray) do
    local seatType = tonumber(seatTypeCount.seatType)
    local count = tonumber(seatTypeCount.count)
    for indexTwo, takeoutRouteDTO in ipairs(takeoutRouteDTOList) do
        local startStation = tostring(takeoutRouteDTO.startStation)
        local endStation = tostring(takeoutRouteDTO.endStation)
        local remainTicketKey = startStation .. "-" .. endStation .. "-" .. seatType
        redis.call('hincrby', KEYS[1], tostring(remainTicketKey), count)
    end
end

return 0